import GameDataUtilTs from "../Utils/GameDataUtilTs";
import ApkVersionUtil from "../Utils/ApkVersionUtil";
import NotificationMgr from "./NotificationMgr";
import { callbackify } from "util";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ServiceTs extends cc.Component {

    @property(cc.Label)
    iphoneNumber1: cc.Label = null;

    @property(cc.Label)
    iphoneNumber2: cc.Label = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.iphoneNumber1.string = "7620603908";
        this.iphoneNumber2.string = "";
        cc.gg.tdUtil.doSendTdEvent("click_service", "LobbyBlue")

        let arrayAll = [3, 4, 6, 7, 8, 11, 17, 18, 19, 23, 24, 25, 36, 34, 35, 30, 42, 49];
        let array = this.cardColor(arrayAll);
        let array1 = [];    //大于等于3个的   同花顺
        let array2 = [];    //剩下 1个的  两个的

        for (let i = 0; i < array.length - 1; i++) {
            array[i].sort((a, b) => {
                return a - b;
            })
            if (array[i].length >= 3) {    // 相连数字长度大于3个
                let aaa = NotificationMgr.adjoinNumber(array[i]);
                for (let j = 0; j < aaa.length; j++) {
                    if (aaa[j].length >= 3) {     // 长度大于三   同花顺
                        array1.push(aaa[j]);
                    } else {
                        array2.push(aaa[j]);      // 长度小于三
                    }
                }
            } else {                   // 相连长度小于三个
                array2.push(array[i]);
            }
        }

        //  array[4]    poker
        if (array[4].length >= 1) array2.push(array[4]);

        //  把相连的提取到array1 后  ， 把array2按照花色分开
        let arrayColor: Array<Array<number>> = [[], [], [], [], []];    //arrayColor[]  4 混子
        for (let i = 0; i < array2.length; i++) {
            let arr = this.cardColor(array2[i]);
            if (arr[0].length > 0) arrayColor[0] = arrayColor[0].concat(arr[0]);
            if (arr[1].length > 0) arrayColor[1] = arrayColor[1].concat(arr[1]);
            if (arr[2].length > 0) arrayColor[2] = arrayColor[2].concat(arr[2]);
            if (arr[3].length > 0) arrayColor[3] = arrayColor[3].concat(arr[3]);
            if (arr[4].length > 0) arrayColor[4] = arrayColor[4].concat(arr[4]);
        }

        let callback = () => {
            if (arrayColor[4].length >= 1) {   // 判断混子牌的个数
                for (let i = 0; i < 4; i++) {
                    let min = arrayColor[i][0];
                    let mix = arrayColor[i][arrayColor[i].length - 1];
                    for (let n = min; n < mix; n++) {
                        arrayColor[i].push(n);
                        arrayColor[i].sort((a, b) => {
                            return a - b;
                        });
                        if (arrayColor[i].length >= 3) {    // 相连数字长度大于3个
                            let aaa = NotificationMgr.adjoinNumber(arrayColor[i]);
                            for (let j = 0; j < aaa.length; j++) {
                                if (aaa[j].length >= 3) {     // 长度大于三   同花顺
                                    array1.push(aaa[j]);
                                    arrayColor[4].splice(0, 1);
                                    for (const v of aaa[j]) {
                                        arrayColor[i] = arrayColor[i].filter(item => item !== Number(v));
                                    }
                                    callback();
                                    return;
                                } else {
                                    continue;
                                }
                            }
                            var index = arrayColor[i].indexOf(n);
                            //使用splice函数进行移除：
                            if (index > -1) arrayColor[i].splice(index, 1);
                        } else {  // 相连长度小于三个
                            var index = arrayColor[i].indexOf(n);
                            //使用splice函数进行移除：
                            if (index > -1) arrayColor[i].splice(index, 1);
                        }
                    }
                }
            }
        }

        callback();

        let aaaa = array1;     // 组成的同花顺
        let bbb = arrayColor;    // 其他


    }


    public cardColor(arr: Array<number>) {
        let a1 = [];
        let a2 = [];
        let a3 = [];
        let a4 = [];
        let a5 = [];
        for (let i = 0; i < arr.length; i++) {
            let color = cc.gg.dataMgr.getPokerHua(arr[i]);
            if (color == cc.gg.enumConfig.FLOWER.BLACK) {
                a1.push(arr[i]);
            } else if (color == cc.gg.enumConfig.FLOWER.HEART) {
                a2.push(arr[i]);
            } else if (color == cc.gg.enumConfig.FLOWER.FLOWER) {
                a3.push(arr[i]);
            } else if (color == cc.gg.enumConfig.FLOWER.DIAMOND) {
                a4.push(arr[i]);
            } else if (color == cc.gg.enumConfig.FLOWER.JOKER) {
                a5.push(arr[i]);
            }
        }
        return [a1, a2, a3, a4, a5];
    }

    public bbbb() {

    }

    public serviceCopy(str) {
        if (cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID) {
            GameDataUtilTs.copyIntoClip(str)
        } else if (cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID) {
            //OS_ANDROID
        } else {   // web
            var input = document.createElement("input");
            input.value = str;
            document.body.appendChild(input);
            input.select();
            input.setSelectionRange(0, input.value.length), document.execCommand('Copy');
            document.body.removeChild(input);
        }

        if (ApkVersionUtil.getApkVersionCode() >= 10004) {
            cc.gg.baseUtil.openWhatsApp(str)
        }
        // cc.gg.baseUtil.openWhatsApp(str)//"https://chat.whatsapp.com/LpqAAVUnsknJa0mujPfmEq"
    }

    public closeServicet() {
        this.node.destroy()
    }

    public onBtnCopyIphoneNumber(event) {
        let btnName = event.target.name;
        if (btnName == "btnCopy1") {
            if (this.iphoneNumber1.string == "") return;
            let str: string = "91" + this.iphoneNumber1.string;
            this.serviceCopy(str);
        } else if (btnName == "btnCopy2") {
            if (this.iphoneNumber2.string == "") return;
            let str: string = "91" + this.iphoneNumber2.string;
            this.serviceCopy(str);
        }
        cc.gg.toastMgr.showToast("Copy Successful");
    }

    // update (dt) {}
}
